

namespace Stee.CAP8.APIGateway.JWTAuthentication.Models
{
    using System;

    public class JWTAccessToken
    {
        /*
         * Sample response
         * =====
         * Response.Content =>
         * "access_token":eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtFaFBNQl80djZMS0duUWEyM1FjMSJ9.eyJpc3MiOiJodHRwczovL2Rldi16dHB4dzMyNS5hdS5hdXRoMC5jb20Iiwic3ViIjoidFp2UkdQbTY5ekdqUDREZ09EeGtvczBtVTJtU0dvUnlAY2xpZW50cyIsImF1ZCI6IkVudm95IGF1dGhlbnRpY2F0aW9uIFJTIiwiaWF0IjoxNTkyMTIwMzY2CJleHAiOjE1OTIyMDY3NjYsImF6cCI6InRadlJHUG02OXpHalA0RGdPRHhrb3MwbVUybVNHb1J5IiwiZ3R5IjoiY2xpZW50LWNyZWRlbnRpYWxzIn0.BOEhUpW3lZoFsRpuSYDF7Wl7qr8arnuUz0OwCVTY7FvwjQkxFUcqtU-WOHGOXHZuwDMqghgKXzC49sNgcf8I6mi-7VsbmSY_lf9vq10mpjyV3_SZUYu4YWkKRTEf3Pb60OB10ibsfsGdxSc1XAHKFGknnBCi7pAyGRcrBQE4vJZAS1Cq4N3TcylEuZqYTLfspRKzSYdGCakKsmRPM2onLD2rgYgLbhRqJz9v4rPG3HFdhn8z0HUnzNuP52cvnGDi63TACNtlLX3RPKOAk5qoet6T1nIx3M2LC2X6bzjtqvTMltCYf1wWo5ZTQe1BYY8mVM3vjHqePL4OotnbJGw,
          * 
          * "expires_in": 86400, //// seconds, equals to 24 hours
          * 
          * "token_type": "Bearer"
          */

        /// <summary>
        /// Gets or sets the access token
        /// </summary>
        public string AccessToken { get; private set; }

        /// <summary>
        /// Gets or sets the token type
        /// </summary>
        public string TokenType { get; private set; }

        /// <summary>
        /// Gets or sets the expiration time
        /// </summary>
        public int ExpiresIn { get; private set; }


        /// <summary>
        /// Initializes a new instance of the JWTAccessToken class
        /// </summary>
        /// <param name="accessToken">JWT access token</param>
        /// <param name="tokenType">Token type</param>
        /// <param name="expiresIn">Expiry time in seconds</param>
        public JWTAccessToken(string accessToken, string tokenType, int expiresIn)
        {
            if (string.IsNullOrEmpty(accessToken))
            {
                throw new ArgumentNullException($"Access token cannot be NULL or empty. Argument => {nameof(accessToken)}");
            }
            else if (string.IsNullOrEmpty(accessToken))
            {
                throw new ArgumentNullException($"Token type cannot be NULL or empty. Argument => {nameof(tokenType)}");
            }
            else if (expiresIn < 1)
            {
                throw new ArgumentException($"Expiration time cannot be zero or less. Argument => {nameof(expiresIn)}");
            }

            this.AccessToken = accessToken;
            this.TokenType = tokenType;
            this.ExpiresIn = expiresIn;
        }

        /// <summary>
        /// Initializes a new instance of the JWTAccessToken class
        /// </summary>
        /// <param name="dynamicToken">Dynamic JSON object</param>
        public JWTAccessToken(dynamic dynamicToken)
        {
            if (dynamicToken == null)
            {
                throw new ArgumentNullException($"Token cannot be NULL. Argument => {nameof(dynamicToken)}");
            }
            else if (string.IsNullOrEmpty((string)dynamicToken.access_token))
            {
                throw new ArgumentNullException($"Access token cannot be NULL or empty. Argument => access_token");
            }
            else if (string.IsNullOrEmpty((string)dynamicToken.token_type))
            {
                throw new ArgumentNullException($"Token type cannot be NULL or empty. Argument => token_type");
            }
            else if ((int)dynamicToken.expires_in < 1)
            {
                throw new ArgumentException($"Expiration time cannot be zero or less. Argument => expires_in");
            }

            this.AccessToken = dynamicToken.access_token;
            this.TokenType = dynamicToken.token_type;
            this.ExpiresIn = dynamicToken.expires_in;
        }
    }
}
